# !/use/bin/env python
# -*- coding:utf-8 -*-

from common.operator_excel import OperatorExcel


class ExcelData:
    def __init__(self):
        self.case_id = -1
        self.case_name = -1
        self.case_conditional = -1
        self.is_execute = -1
        self.url = -1
        self.params = -1
        self.result = -1
        self.expect = -1

    """
    @给url、post、params、expect、actual在第几行赋值
    """

    def set_field_index(self, sheet_name, excel_path):
        if self.case_id is -1:
            self.case_id = OperatorExcel.get_col_by_title(excel_path, sheet_name, "case_id")
            self.case_name = OperatorExcel.get_col_by_title(excel_path, sheet_name, "用例标题")
            self.case_conditional = OperatorExcel.get_col_by_title(excel_path, sheet_name, "前置条件")
            self.is_execute = OperatorExcel.get_col_by_title(excel_path, sheet_name, "是否执行")
            self.url = OperatorExcel.get_col_by_title(excel_path, sheet_name, "url")
            self.params = OperatorExcel.get_col_by_title(excel_path, sheet_name, "请求参数")
            self.expect = OperatorExcel.get_col_by_title(excel_path, sheet_name, "Expect")
            self.result = OperatorExcel.get_col_by_title(excel_path, sheet_name, "result")

    """
    将list 转成 dictionary
    """

    def trans_to_dict(self, path, sheet_name, row_info):
        self.set_field_index(sheet_name, path)

        row_dict = {}
        if self.case_id - 1 >= 0:
            row_dict.update({'case_id': row_info[self.case_id - 1]})
        else:
            row_dict.update({'case_id': -1})
        if self.case_name - 1 >= 0:
            row_dict.update({'case_name': row_info[self.case_name - 1]})
        else:
            row_dict.update({'case_name': -1})
        if self.case_conditional - 1 >= 0:
            row_dict.update({'case_conditional': row_info[self.case_conditional - 1]})
        else:
            row_dict.update({'case_conditional': -1})
        if self.is_execute - 1 >= 0:
            row_dict.update({'is_execute': row_info[self.is_execute - 1]})
        else:
            row_dict.update({'is_execute': -1})
        if self.url - 1 >= 0:
            row_dict.update({'url': row_info[self.url - 1]})
        else:
            row_dict.update({'url': -1})
        if self.params - 1 >= 0:
            row_dict.update({'params': row_info[self.params - 1]})
        else:
            row_dict.update({'params': -1})
        if self.result - 1 >= 0:
            row_dict.update({'result': row_info[self.result - 1]})
        else:
            row_dict.update({'result': -1})
        if self.expect - 1 >= 0:
            row_dict.update({'expect': row_info[self.expect - 1]})
        else:
            row_dict.update({'expect': -1})

        return row_dict

    # 处理参数(拆分)
    def process_params(self, path, sheet_name, row_info):
        message = self.trans_to_dict(path, sheet_name, row_info)

        # excel传参解析
        if message['params'] is None:
            return str(message) + "______解析请求传参为空，请确认是否传参"
        else:
            temp_params = message['params'].split(';')

        # 步骤1==================================================================
        params = {}
        for kv in temp_params[0].split(','):
            temp = kv.split('=')
            params.update({temp[0]: temp[1]})
        message.update({'params': params})
        return message

    # 有传参(请求参数格式：a=m,b=c,n=d)
    def process_row_data(self, excel_path, sheet_name):
        test_data = []
        all_rows_info = OperatorExcel.read_all_excel(excel_path, sheet_name)

        for row_info in all_rows_info:
            data = self.process_params(excel_path, sheet_name, row_info)
            test_data.append(data)
        return test_data

    # 处理没有传参(请求参数不传)
    def no_process_params(self, path, sheet_name, row_info):

        message = self.trans_to_dict(path, sheet_name, row_info)

        # excel传参解析
        if message['params'] is not None:
            temp_params = message['params'].split(';')
            params = {}
            for kv in temp_params[0].split(','):
                temp = kv.split('=')
                params.update({temp[0]: temp[1]})
                message.update({'params': params})

        return message

    # 循环读取每一行数据，没有传参(请求参数不传)
    def non_row_data(self, excel_path, sheet_name):
        test_data = []
        all_rows_info = OperatorExcel.read_all_excel(excel_path, sheet_name)

        for row_info in all_rows_info:
            data = self.no_process_params(excel_path, sheet_name, row_info)
            test_data.append(data)
        return test_data

    # 直接引用传参,不处理(请求参数格式：{"a":"b","c":"d","m":"n"})
    def json_params(self, path, sheet_name, row_info):
        message = self.trans_to_dict(path, sheet_name, row_info)
        # excel传参解析
        if '清除数据' in message['case_name'] or '插入数据' in message['case_name']:
            temp_params = message['params'].split(';')
            params = {}
            for kv in temp_params[0].split(','):
                temp = kv.split('=')
                params.update({temp[0]: temp[1]})
                message.update({'params': params})
        return message

    # 循环读取每一行数据，(请求参数格式：{"a":"b","c":"d","m":"n"})
    def json_row_data(self, excel_path, sheet_name):
        # 自动替换路径名称
        excel_path = excel_path.replace("test_case", "test_case_excel").replace(".py", ".xlsx")
        test_data = []
        all_rows_info = OperatorExcel.read_all_excel(excel_path, sheet_name)

        for row_info in all_rows_info:
            # 装载用例,判断是否载入用例 [3] = is_execute
            if row_info[3]:
                data = self.json_params(excel_path, sheet_name, row_info)
                test_data.append(data)

        return test_data


if __name__ == "__main__":
    a = ExcelData()
    print(a.case_id)
    # a.json_row_data(r"C:\Users\Administrator\PycharmProjects\shall_buy_test\test_case\goods_service\goods
    # \test_goods_detail.py", "Sheet1")
